package gates;

/**
 * Composite Grover gate.
 * Basically it is 4 operations: oracle, H, NandAll, H.
 * where the oracle marks the searched state, H is a hadamard transform of all qubits and NandAll is
 * a function does the nand operation on all states (flips all states except ground state).
 * This method was taken from <a href="http://www.hpl.hp.com/breweb/quiprocone/Protected/Lecture_6.htm">Deutch's lectures on quantum computing</a>
 * and from <it>An Introduction To Quantum Computing: Kaye et al</it>
 * @author Jan Zaucha, Alex Bush
 *
 */
public class GroverComposite extends CompositeGate {

	/**
	 * Construct the grover gate. Calls the super class Composite Gate
	 * @param searched Element that is searched in the algorithm
	 */
	protected GroverComposite(int searched, int numQubits) {
		super(getGates(searched, numQubits));
	}

	

	/**
	 * Private method to provide the array of gates used in the Grover algorithm
	 * @param searched Element that is searched
	 * @return groverIteration Array of gates creating the grover iteration.
	 */
	private static Gate[] getGates(int searched, int numQubits){
		Gate oracle = Factory.createOracle(searched);
		Gate hadamardTransform = Factory.createHadamard(-1);
		Gate nand = Factory.createNand(numQubits);
		Gate [] groverIteration = new Gate [4];
		groverIteration[0] = oracle;
		groverIteration[1] = hadamardTransform;
		groverIteration[2] = nand;
		groverIteration[3] = hadamardTransform;
		return groverIteration;
		
}
	
	/**
	 * Return the name of this gate
	 */
	public String getName() {
		return "Grover Iteration";
	}
}
